package edu.hunan.cxb18.car;
/**
 *
 */
import edu.hunan.cxb18.pojo.Product;

import java.util.ArrayList;
import java.util.List;


/**
 * @author chenheng
 *
 * 2019年10月16日 下午1:27:23
 */
public class Car {
    private double total;
    private List<CarItem> items;
    /**
     * @return the total
     */
    public Car() {
        items=new ArrayList<>();
    }
    public double getTotal() {
        total=0;
        for (int i = 0; i < items.size(); i++) {
            CarItem item=items.get(i);
            total+=item.getQty()*item.getGoods().getMarketPrice();
        }
        return total;
    }
    /**
     * @param total the total to set
     */
    public void setTotal(double total) {
        this.total = total;
    }
    /**
     * @return the items
     */
    public List<CarItem> getItems() {
        return items;
    }
    /**
     * @param items the items to set
     */
    public void setItems(List<CarItem> items) {
        this.items = items;
    }
    public void delete(int pid) {
        for (int i = 0; i < items.size();i++) {
            CarItem item=items.get(i);
            if(item.getGoods().getId()==pid) {
                items.remove(i);
            }
        }
    }
    /**
     * @param g1
     * @param i
     */
    public void add(Product goods, int qty) {
        for (int i = 0; i < items.size(); i++) {
            CarItem item = items.get(i);
            //该商品已经在购物车中
            if (item.getGoods().getId() == goods.getId()) {
                item.setQty(item.getQty() + qty);
                return;
            }
        }
        CarItem item = new CarItem();
        item.setGoods(goods);
        item.setQty(qty);
        items.add(item);
    }
    /**
     * @param id
     * @param qty
     */
    public void modify(int pid, int qty) {
        for (int i = 0; i < items.size(); i++) {
            CarItem item=items.get(i);
            if(item.getGoods().getId()==pid) {
                item.setQty(qty);
            }
        }
    }
    /**
     *
     */
    public void deleteAll() {
        System.out.println(items.size());
        items.clear();
        System.out.println(items.size());
    }

}
